package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.weipai.springboot.model.Storage;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:51
 * <p>
 * 仓储管理类
 */
@RestController
public class StorageController {

    @Autowired
    StorageService storageService;

    /**
     * 仓库分页查询
     *
     * @param page   页码
     * @param size   每页条数
     * @param draw   前台参数，照样返回chang
     * @param search 搜索，如果无搜索此值为paging
     * @param sort   time
     * @param way    desc，asc，noway
     */
    @GetMapping("/storage/list/{page}/{size}/{draw}/{search}/{sort}/{way}")
    public Response list(@PathVariable("page") Integer page,
                         @PathVariable("size") Integer size,
                         @PathVariable("draw") String draw,
                         @PathVariable("search") String search,
                         @PathVariable("sort") String sort,
                         @PathVariable("way") String way) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, size);
        Response response = storageService.selectAllByStorageDel(0, search, page, size, sort, way, draw);
        return response;
    }

    /**
     * 查询商品各个仓储下面的库存量
     * @param skuNumber
     * @return
     */
    @GetMapping("/storage/getStorageGoodsNum/{skuNumber}")
    public Response getStorageGoodsNum(@PathVariable("skuNumber") String skuNumber){
        return storageService.getStorageGoodsNum(skuNumber);
    }
    
    

    /**
     * 添加仓储
     *
     * @param storage
     * @return
     */
    @PostMapping("/storage/save")
    public Response save(Storage storage) {
        return storageService.save(storage);
    }

    /**
     * 修改仓储
     *
     * @param storage
     * @return
     */
    @PostMapping("/storage/update")
    public Response update(Storage storage) {
        return storageService.update(storage);
    }

    /**
     * 查询仓储详情
     *
     * @param storageId:仓储id
     * @return
     */
    @GetMapping("/storage/getStorageById/{id}")
    public Response getStorageById(@PathVariable("id") String storageId) {
        return storageService.getStorageById(storageId);
    }

    /**
     * 删除仓库
     *
     * @param storageId:仓储id
     * @return
     */
    @DeleteMapping("/storage/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String storageId) {
        return storageService.deleteById(storageId);
    }

    /**
     * 批量删除
     *
     * @param id:仓储的id集合
     * @return
     */
    @DeleteMapping("/storage/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id") String[] id) {
        return storageService.batchDeleteById(id);
    }

    /**
     * 更改仓储的状态
     *
     * @param storageId:仓储id
     * @return
     */
    @GetMapping("/storage/changeStatus/{id}/{status}")
    public Response changeStatus(@PathVariable("id") String storageId,
                                            @PathVariable("status") Integer status) {
        return storageService.changeStatus(storageId, status);
    }


    /**
     * 分页查询
     * 缺货设置
     *
     * @param page 页码
     * @param size 每页条数
     * @param draw 前台参数，照样返回chang
     */
    @GetMapping("/storage/listNoGoods/{storageId}/{page}/{size}/{draw}")
    public Response listNoGoods(@PathVariable("storageId") String storageId,
                                @PathVariable("page") Integer page,
                                @PathVariable("size") Integer size,
                                @PathVariable("draw") String draw) {
        return storageService.listNoGoods(storageId, page, size, draw);
    }

    /**
     * 根据位置定位最近的仓储
     * @param longitude
     * @param latitude
     * @return
     */
    @PostMapping("/storage/locateStorage")
    public Response locateStorage(String longitude, String latitude){
        return storageService.locateStorage(longitude, latitude);
    }
}
